package Servlst;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.File;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "UploadServlet", value = "/UploadServlet")
public class UploadServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //判断是不是一个上传文件
        boolean multipartContent = ServletFileUpload.isMultipartContent(request);
        if (!multipartContent) {
            throw new RuntimeException("不是一个上传类型");
        }

        //相关文件和文本的内容

        //上传的解析对象
        DiskFileItemFactory factory = new DiskFileItemFactory();

        //上传文件
        ServletFileUpload upload = new ServletFileUpload(factory);

        // 设置上传文件的大小限制
        upload.setSizeMax(1024 * 1024 * 10);

        //解决乱码
        upload.setHeaderEncoding("utf-8");


        //获取所有的请求
        try {
            List<FileItem> fileItems = upload.parseRequest(request);
            for (FileItem item : fileItems) {

                //判断提交的类型
                //普通类型
                if (item.isFormField()) {
                    String fieldName = item.getFieldName();//username
                    String string = item.getString("utf-8");//username的值

                    System.out.println(fieldName);
                    System.out.println(string);
                } else {
                    //上传类型
                    String fieldName = item.getFieldName();//file
                    String name = item.getName();//获取上传文件的名字

                    System.out.println(fieldName);
                    System.out.println(name);

                    //开始上传
                    //上传的位置 目录是否存在 上传项目所在位置
                    String realPath = this.getServletContext().getRealPath("/上传的位置");

                    File file = new File(realPath);
                    if (!file.exists()) {
                        //不存在就创建
                        file.mkdirs();
                    }

                    System.out.println(file.getPath());
                    try {
                        //把文件放在刚刚创建的目录下
                        item.write(new File(file, name));
                        response.sendRedirect("Main.jsp");
                        try {
                            //成功弹出上传的文件夹
                            Runtime.getRuntime().exec("explorer A:\\ALL_Study\\Tomcat\\apache-tomcat-8.5.83\\webapps\\New_Login_war\\上传的位置");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }

            }


        } catch (FileUploadException e) {
            throw new RuntimeException(e);
        }


    }
}
